package io.gitee.thinkbungee.utils.mongo;

import io.gitee.thinkbungee.config.DBConfig;
import io.gitee.thinkbungee.entity.mongo.MongoDefinition;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 使用本地缓存来管理mongo
 *
 * @author chenyichang
 * @date 2022/3/9
 */
public class MongoManager {
    /**
     * mongo本地缓存
     */
    private static Map<String, MongoDefinition> mongoCache = new ConcurrentHashMap<>();

    /**
     * 获取缓存中的表的所有信息
     *
     * @param tableName
     * @return
     */
    public static MongoDefinition getInfo(String tableName) {
        return mongoCache.getOrDefault(tableName, null);
    }

    /**
     * 将表的信息放入本地缓存
     *
     * @param tableName
     * @param mongoDefinition
     * @return
     */
    public static MongoDefinition putInfo(String tableName, MongoDefinition mongoDefinition) {
        return mongoCache.put(tableName, mongoDefinition);
    }

    public static boolean isMongo() {
        return DBConfig.isMongo();
    }

    public static Map<String, MongoDefinition> getMongoCache() {
        return mongoCache;
    }
}
